package com.mars.boss.weixin.web.contrller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.mars.boss.weixin.entity.WeixinNewsArticle;
import com.mars.boss.weixin.entity.WeixinNewsCategory;
import com.mars.boss.weixin.exception.WeixinBizException;
import com.mars.boss.weixin.service.WeixinNewsArticleService;
import com.mars.boss.weixin.service.WeixinNewsCategoryService;
import com.mars.web.common.pojo.DataGrid;
import com.mars.web.common.support.BaseController;

@Controller
@RequestMapping("/weixin/news")
public class WeixinNewsController extends BaseController {
	
	@Autowired
	private WeixinNewsCategoryService serviceNewsCategoryService ;
	
	@Autowired
	private WeixinNewsArticleService serviceNewsArticleService ;
	
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main() {
		return viewName("main") ;
	}
	
	@RequestMapping(value="/form_category", method=RequestMethod.GET)
	public String form(Long id, Model model) {
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		return viewName("form_category") ;
	}

	@RequestMapping(value="/form_article", method=RequestMethod.GET)
	public String form(Long id, Long categoryId, Model model) {
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		if(null != categoryId) {
			model.addAttribute("categoryId", categoryId) ;
		}
		return viewName("form_article") ;
	}
	
	@RequestMapping(value="/add/category", method=RequestMethod.POST)
	@ResponseBody
	public Object addCategory(WeixinNewsCategory entity) {
		try {
			this.serviceNewsCategoryService.insert(entity) ;
		} catch (WeixinBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		
		return setResponseResultAddOk();
	}
	
	@RequestMapping(value="/add/article", method=RequestMethod.POST)
	@ResponseBody
	public Object addArticle(WeixinNewsArticle entity) {
		try {
			this.serviceNewsArticleService.insert(entity) ;
			System.out.println(entity);
		} catch (WeixinBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		
		return setResponseResultAddOk();
	}
	
	@RequestMapping(value="/delete/category/{ids}", method=RequestMethod.GET)
	@ResponseBody
	public Object deleteCategory(@PathVariable String ids) {
		try {
			this.serviceNewsCategoryService.deleteByIds(ids) ;
		} catch (WeixinBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		return setResponseResultDeleteOk();
	}
	
	@RequestMapping(value="/delete/article/{ids}", method=RequestMethod.GET)
	@ResponseBody
	public Object deleteArticle(@PathVariable String ids) {
		try {
			this.serviceNewsArticleService.deleteByIds(ids) ;
		} catch (WeixinBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		return setResponseResultDeleteOk();
	}
	
	@RequestMapping(value="/update/category", method=RequestMethod.POST)
	@ResponseBody
	public Object updateCategory(WeixinNewsCategory entity) {
		try {
			this.serviceNewsCategoryService.update(entity) ;
		} catch (WeixinBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		return setResponseResultUpdateOk();
	}
	
	@RequestMapping(value="/update/article", method=RequestMethod.POST)
	@ResponseBody
	public Object updateArticle(WeixinNewsArticle entity) {
		try {
			this.serviceNewsArticleService.update(entity) ;
		} catch (WeixinBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		return setResponseResultUpdateOk();
	}
	
	@RequestMapping(value="/get/category/{id}", method=RequestMethod.GET)
	@ResponseBody
	public WeixinNewsCategory getCategory(@PathVariable Long id) {
		return this.serviceNewsCategoryService.selectById(id) ;
	}
	
	@RequestMapping(value="/get/article/{id}", method=RequestMethod.GET)
	@ResponseBody
	public WeixinNewsArticle getArticle(@PathVariable Long id) {
		return this.serviceNewsArticleService.selectById(id) ;
	}
	
	@ResponseBody
	@RequestMapping(value="/category/list", method=RequestMethod.GET)
	public Object list(WeixinNewsCategory entity, Integer page, Integer rows) {
		List<WeixinNewsArticle> articlesByCategoryId = this.serviceNewsArticleService.getArticlesByCategoryId(1013L) ;
		System.out.println(articlesByCategoryId);
		try {
			PageInfo<WeixinNewsCategory> selectPage = this.serviceNewsCategoryService.selectPage(getPageParam(page, rows), entity, getPageParam().getOrderby()) ;
			DataGrid<WeixinNewsCategory> dg = new DataGrid<WeixinNewsCategory>() ;
			dg.setRows(selectPage.getList());
			dg.setTotal(selectPage.getTotal());
			
			return dg ;
		} catch (WeixinBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/article/list", method=RequestMethod.GET)
	public Object list(WeixinNewsArticle entity, Integer page, Integer rows) {
		try {
			PageInfo<WeixinNewsArticle> selectPage = this.serviceNewsArticleService.selectPage(getPageParam(page, rows), entity, getPageParam().getOrderby()) ;
			DataGrid<WeixinNewsArticle> dg = new DataGrid<WeixinNewsArticle>() ;
			dg.setRows(selectPage.getList());
			dg.setTotal(selectPage.getTotal());
			
			return dg ;
		} catch (WeixinBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
	}
}
